const fs = require("fs");
const path = require("path");
const os = require("os");

const rootPath = fs.realpathSync(process.cwd());

/**
 * {path: string, name: string}
 * @param target
 * @param result
 */
function fetchMarkdownFiles(target, result = []) {
    // console.log(target)
    const stat = fs.statSync(target);
    if (stat.isDirectory()) {
        const readRes = fs.readdirSync(target);
        for (const rs of readRes) {
            const subTarget = target + '/' + rs;
            fetchMarkdownFiles(subTarget, result);
        }
    } else if (stat.isFile()){
        const file = path.resolve(target);
        const ext = path.extname(file);
        if(ext === '.md') {
            result.push({
                dir: path.dirname(target),
                path: path.resolve(target),
                name: path.basename(target, '.md')
            });
        }
    }
}

module.exports = {
    fetchMarkdownFiles,
    rootPath
}
